package com.fenghongzhang.day010;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

/**
 * 对指定的数据库增删改查
 */
public class TestContentProvider extends ContentProvider {
    private SQLiteDatabase db;
    public TestContentProvider() {
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        int test = db.delete("test", selection, selectionArgs);
        return test;
    }

    @Override
    public String getType(Uri uri) {
        // TODO: Implement this to handle requests for the MIME type of the data
        // at the given URI.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    //uri :别人访问你的数据的地址.
    @Override
    public Uri insert(Uri uri, ContentValues values) {
        db.insert("test",null,values);
        return uri;
    }

    //打开要操作的数据库
    @Override
    public boolean onCreate() {
        MySQlHelp mySQlHelp = new MySQlHelp(getContext(), "a1806.db", null, 1);
        db = mySQlHelp.getReadableDatabase();
        if(db != null){
            return true;
        }
        return false;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        Cursor test = db.query("test", projection, selection, selectionArgs, null, null, sortOrder);
        return test;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        int test = db.update("test", values, selection, selectionArgs);
        return test;
    }
}
